/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune.changelog.pipe;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

abstract class PipeState {
  Log LOG = LogFactory.getLog(PipeState.class);

  abstract void init(Pipe.Context ctx) throws IOException;

  abstract String getStateName();

  boolean fini(Pipe.Context ctx, int bytes) throws IOException {
    LOG.warn("Illegal invocation of fini");
    return true;
  }

  boolean writeToNext(Pipe.Context ctx) throws IOException {
    LOG.warn("Illegal invocation of writeToNext");
    return true;
  }

  boolean writeToPrev(Pipe.Context ctx) throws IOException {
    LOG.warn("Illegal invocation of writeToPrev");
    return true;
  }

  boolean readFromNext(Pipe.Context ctx) throws IOException {
    LOG.warn("State : " + getStateName() + ", Illegal invocation of readFromNext");
    try {
      Thread.sleep(10000);
    } catch (Exception e) {
    }
    return true;
  }

  boolean readFromPrev(Pipe.Context ctx) throws IOException {
    LOG.warn("Illegal invocation of readFromPrev");
    return true;
  }

  boolean connectToNext(Pipe.Context ctx) throws IOException {
    LOG.warn("Illegal invocation of connectToNext");
    return true;
  }

  boolean accept(Pipe.Context ctx) {
    LOG.warn("Illegal invocation of accept");
    return true;
  }

  void shutdown(Pipe.Context ctx) {
    LOG.warn("Illegal invocation of shutdown");
  }

  abstract public void close(Pipe.Context ctx) throws IOException;
}
